package com.example.cnotebook.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.cnotebook.EditNoteActivity;
import com.example.cnotebook.R;
import com.example.cnotebook.SearchActivity;
import com.example.cnotebook.adapter.HomeListViewAdapter;
import com.example.cnotebook.pojo.CategoryInfo;
import com.example.cnotebook.pojo.NoteInfo;
import com.example.cnotebook.util.OKHttpUtil;
import com.example.cnotebook.util.UserConstant;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class HomeFragment extends BaseFragment implements AdapterView.OnItemClickListener, View.OnClickListener {
    private ImageView ivSearch;
    private ListView listview;
    private TextView tvEmpty;
    private Context context;

    private ArrayList<NoteInfo> noteInfos = new ArrayList<>();
    private HomeListViewAdapter adapter;

    private SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月");

    private Gson gson = new Gson();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home_layout, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        context = getContext();
        ivSearch = view.findViewById(R.id.iv_search);
        listview = view.findViewById(R.id.listview);
        tvEmpty = view.findViewById(R.id.tv_empty);

        ivSearch.setOnClickListener(this);

        adapter = new HomeListViewAdapter(context, noteInfos);
        listview.setAdapter(adapter);
        listview.setEmptyView(tvEmpty);
        listview.setOnItemClickListener(this);
        initData();
    }

    private void initData() {
        this.noteInfos.clear();
        ArrayList<NoteInfo> data = UserConstant.noteInfos;
        ArrayList<CategoryInfo> categoryInfos = UserConstant.categoryInfos;
        Date date = null;
        for (int i = 0; i < data.size(); i++) {
            NoteInfo noteInfo = data.get(i);
            noteInfo.setCreateDateTime(noteInfo.getCreateDate());
            if (noteInfo.getCategoryId() == -1) noteInfo.setCategoryName("未分类");
            if (i == 0) {
                date = noteInfo.getCreateDate();
            } else {
                if (format.format(noteInfo.getCreateDate()).equals(format.format(date))) {
                    noteInfo.setCreateDate(null);
                } else {
                    date = noteInfo.getCreateDate();
                }
            }
            for (CategoryInfo categoryInfo : categoryInfos) {
                if (categoryInfo.getId().equals(noteInfo.getCategoryId())) {
                    noteInfo.setCategoryName(categoryInfo.getName());
                }
            }
            this.noteInfos.add(noteInfo);
        }
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(context, EditNoteActivity.class);
        intent.putExtra("nodeId", noteInfos.get(position).getId());
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        startActivity(new Intent(context, SearchActivity.class));
    }

    private void queryNote() {
        OKHttpUtil.get("/note/list/" + UserConstant.userInfo.getId(), new OKHttpUtil.CallBack() {
            @Override
            public void onSuccess(JSONObject response) throws JSONException {
                JSONArray jsonArray = response.getJSONArray("data");
                UserConstant.noteInfos.clear();
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    UserConstant.noteInfos.add(gson.fromJson(jsonObject.toString(), NoteInfo.class));
                }
                initData();
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {

            }
        }, (Activity) context);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (UserConstant.homeIsSaveData) {
            UserConstant.homeIsSaveData = false;
            queryNote();
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        System.out.println(hidden);
    }
}
